/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(honeysoft@126.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.stascore.serviceimpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.teasoft.bee.osql.Condition;
import org.teasoft.bee.osql.FunctionType;
import org.teasoft.bee.osql.Op;
import org.teasoft.bee.osql.SuidRich;
import org.teasoft.honey.osql.core.ConditionImpl;

import com.aiteasoft.school.stascore.entity.StaScore;
import com.aiteasoft.school.stascore.service.StuStaScoreService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-29 18:15:43
 */
@Service
public class StuStaScoreServiceImpl  implements StuStaScoreService{  

	@Autowired
	SuidRich suidRich;

	@Override
	public StaScore process(StaScore entity) { 
		//Add the business logic if need. 放一些公共设置
		
		//TODO  根据不同角色设置.
		if("classOrder".equals(entity.getOrderType()))
		   entity.setClassno("201");
		
		return entity;
	}
	
	@Override
	public boolean isWrongBizForUpdate(Result result, StaScore entity) {
		//Add the business logic if need.

		return false;
	}

	
	public PageWarp countAndSelect(StaScore entity, int start, int size) {
	    Condition condition = getCondition(entity.getOrderType());
//	    entity.setOrderType(null); //不用于解析
	    
		List<StaScore> list=suidRich.select(entity, condition);
		int total= list==null? 0 : list.size();
	    
//		int total = suidRich.count(entity, condition); //因最高分等是汇总来的,不是表直接对应字段. //TODO
		
		if (total > size) {
			condition.start(start);
			condition.size(size);
			list = suidRich.select(entity, condition);
		}
		
		return new PageWarp(total,list);
	}
	
	private Condition getCondition(String orderType) {
		if ("classOrder".equals(orderType))
			return classOrder();
		else
			return gradeOrder();
	}
	
	//年级排名
	private Condition classOrder() {
		Condition condition=new ConditionImpl();
		
		condition
		.selectField("classno,term,examno,subject")
		.selectFun(FunctionType.MAX, "score","maxscore")
		.selectFun(FunctionType.AVG, "score","avgscore")
		.selectFun(FunctionType.MIN, "score","minscore");
		
		condition.op("status", Op.eq, "FIN"); //TODO 统计已完成状态的
		
		condition
		.groupBy("classno,term,examno,subjectno,subject")
		.orderBy("classno,term,examno,subjectno")
		;
		
		return condition;
	}
	
	//班级排名  文理分科
	private Condition gradeOrder() {
		Condition condition=new ConditionImpl();
		
		condition
		.selectField("term,examno,subject")
		.selectFun(FunctionType.MAX, "score","maxscore")
		.selectFun(FunctionType.AVG, "score","avgscore")
		.selectFun(FunctionType.MIN, "score","minscore");
		
		condition.op("status", Op.eq, "FIN"); //TODO 统计已完成状态的
		
		//TODO 根据用户角色添加对班级的过滤.
		//老师可以查任教班级,学生查自己所在班级. 教务处不过滤. 
		
		condition
		.groupBy("examno,subjectno,subject")
		.orderBy("examno,subjectno")
		;
		
		return condition;
	}

}